package no.awerup.armory_miner.collectors;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathException;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import no.awerup.armory_miner.AMClient;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import domain.Char;
import domain.Item;

public class CharCollector extends Collector {

	public void process(Exchange exchange) throws Exception {
		String inBody = (String) exchange.getIn().getBody( String.class );
//		Char cha = (Char) exchange.getIn().getBody( Char.class );
		System.out.println( inBody);
		
		Char cha = (Char) xstream.fromXML(inBody);
		System.out.println( cha.getName() + " of " + cha.getServerName() );

		collectCharData( cha.getName(), cha.getServerName());
	}

	void collectCharData(String charName, String realm) throws ParserConfigurationException, SAXException, IOException, XPathException {
		AMClient amClient = createAMClient();    
		System.out.println( "Getting char info for '" + charName + "' of '" + realm + "'" );
    	String gs = amClient.getChar(realm, charName );
    	System.out.println( gs );
    	
    	Char cha = parseCharXMLtoChar( gs );
    	
	}

	Char parseCharXMLtoChar(String charXML) throws ParserConfigurationException, SAXException, IOException, XPathException { 
		DocumentBuilder builder = createDocumentBuilder();
		Document doc = builder.parse(new InputSource(new StringReader(charXML)));
		
		XPathFactory factory = XPathFactory.newInstance();
		XPath xpath = factory.newXPath();
		
		javax.xml.xpath.XPathExpression classNameXpath = xpath.compile("//characterInfo/character/@class");
		javax.xml.xpath.XPathExpression classIdXpath = xpath.compile("//characterInfo/character/@classId");
		javax.xml.xpath.XPathExpression factionNameXpath = xpath.compile("//characterInfo/character/@faction");
		javax.xml.xpath.XPathExpression factionIdXpath = xpath.compile("//characterInfo/character/@factionId");
		javax.xml.xpath.XPathExpression genderNameXpath = xpath.compile("//characterInfo/character/@gender");
		javax.xml.xpath.XPathExpression genderIdXpath = xpath.compile("//characterInfo/character/@genderId");
		javax.xml.xpath.XPathExpression raceNameXpath = xpath.compile("//characterInfo/character/@race");
		javax.xml.xpath.XPathExpression nameXpath = xpath.compile("//characterInfo/character/@name");
		javax.xml.xpath.XPathExpression levelXpath = xpath.compile("//characterInfo/character/@level");
		javax.xml.xpath.XPathExpression lastModifiedXpath = xpath.compile("//characterInfo/character/@lastModified");
		javax.xml.xpath.XPathExpression realmXpath = xpath.compile("//characterInfo/character/@realm");		
		javax.xml.xpath.XPathExpression battleGroupXpath = xpath.compile("//characterInfo/character/@battleGroup");
		
		javax.xml.xpath.XPathExpression itemsXpath = xpath.compile("//characterInfo/characterTab/items/item");
		
		// Handle items
		NodeList itemsNodeList = (NodeList) itemsXpath.evaluate(doc, XPathConstants.NODESET);		

		for (int i = 0; i < itemsNodeList.getLength(); i++) {
			Node itemNode = itemsNodeList.item(i);
			Item item = new Item();
			
		}
		
		
		
		
		String clazz = (String) classNameXpath.evaluate( doc, XPathConstants.STRING );
//		javax.xml.xpath.XPathExpression characterNamesXpath = xpath.compile("//guildInfo/guild/members/character/@name");
		System.out.println( clazz );
		
		return null;
	}

}
